/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package apirose;

import apirose.APIEnseignant;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author v-tech_master
 */
public class Enseignant_MySQL extends BD_MySQL implements APIEnseignant {

    private Request_MySQL bdd;
    private int idPromotion;
    
    Enseignant_MySQL(Request_MySQL request, int idPromo) {
        this.bdd = request;
        this.idPromotion = idPromo;
    }

    //TODO
    @Override
    public ArrayList<Integer> getListeIdSeanceEnseignantBDD(int idEnseignant) {
        ArrayList<Integer> listeId = new ArrayList<Integer>();
        
        String req = "SELECT id FROM V_Infos_Seance_Pomotion WHERE idIntervenant = "+idEnseignant+" AND idPromotion = "+this.idPromotion+";";
        
        ResultSet rs = bdd.executer_requete(req);
        try {
            while(rs.next()){
                listeId.add(rs.getInt("id"));
            }
        } catch (SQLException ex) {
            Logger.getLogger(Enseignant_MySQL.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        return listeId;
    }

    @Override
    public ArrayList<Integer> getListeIdContrainteDispoEnseignantBDD(int idEnseignant) {
        ArrayList<Integer> listeId = new ArrayList<Integer>();
        
        String req = "SELECT id FROM Creneau_Intervenant WHERE idIntervenant = "+idEnseignant+";";
        
        ResultSet rs = bdd.executer_requete(req);
        try {
            while(rs.next()){
                listeId.add(rs.getInt("id"));
            }
        } catch (SQLException ex) {
            Logger.getLogger(Enseignant_MySQL.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        return listeId;
    }

    @Override
    public Date getDateDebutContrainteDispoBDD(int idCreanaux_intervenant) {
        Date date = null;
        
        String req = "SELECT tsDebut FROM Creneau_Intervenant WHERE id = "+idCreanaux_intervenant+";";
        
        ResultSet rs = bdd.executer_requete(req);
        try {
            while(rs.next()){
                Timestamp dateamps = rs.getTimestamp("tsDebut");
                date = new Date(dateamps.getTime());
            }
        } catch (SQLException ex) {
            Logger.getLogger(Edt_MySQL.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        return date;
    }

    @Override
    public Date getDateFinContrainteDispoBDD(int idCreanaux_intervenant) {
        Date date = null;
        
        String req = "SELECT tsFin FROM Creneau_Intervenant WHERE id = "+idCreanaux_intervenant+";";
        
        ResultSet rs = bdd.executer_requete(req);
        try {
            while(rs.next()){
                Timestamp dateamps = rs.getTimestamp("tsFin");
                date = new Date(dateamps.getTime());
            }
        } catch (SQLException ex) {
            Logger.getLogger(Edt_MySQL.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        return date;
    }

    //Boolean : true ajout d'une nouvelle disponibilité
    //          false suppression d'une disponibilité
    @Override
    public int saveContrainteDispo(int idEnseignant, Date deb, Date fin, boolean ajoute) {
        Timestamp tsDebut = new Timestamp(deb.getTime());
        Timestamp tsFin = new Timestamp(fin.getTime());
        String req;

        if(ajoute){
            req = "INSERT INTO Creneau_Intervenant (tsDebut, tsFin, idIntervenant) VALUES ('"+tsDebut+"', '"+tsFin+"', "+idEnseignant+");";
        }else{
            req = "DELETE FROM Creneau_Intervenant WHERE idIntervenant="+idEnseignant+" AND tsDebut='"+tsDebut+"' AND tsFin='"+tsFin+"';";
        }
        
        return bdd.executer_update(req);
    }
    
}
